package com.project.contorller;

import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    public IUserService service;
    @RequestMapping("findAll")
    public List<UserBean> findAll(){
        return service.findAll();
    }
    @RequestMapping("login")
    public String  login( String userName, String pwd, HttpServletRequest request){

        UserBean user = service.login(userName,pwd);
        if(user != null){
            request.getSession().setAttribute("loginUser",user);
            return "ok";
        }
        return "no";
    }
    @RequestMapping("delUser")
    public  boolean delUser(int id,UserBean userBean){
     return service.delUser(id,userBean);
    }
    @RequestMapping("findById")
    public  UserBean findById( int id){
        return  service.findById(id);
    };
    @RequestMapping("updateUserPwd")
   public String updateUserPwd(int id,String pwd){
        service.updateUserPwd(id,pwd);
        return "ok";
    }
    @RequestMapping("addUser")
    public boolean  addUser(UserBean userBean){
      return   service.addUser(userBean);
    }
    @RequestMapping("updateGrade")
    void updateGrade(int id ,String level){
        service.updateGrade(id,level);
    }

    @RequestMapping("getLoginUser")
    public UserBean getLoginUser(HttpServletRequest req){
        UserBean user = (UserBean)req.getSession().getAttribute("loginUser");
        return user;
    }

}
